package com.hzsoft.util;


import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * mybatis 代码生成工具 生成实体、mapper接口、sql配置文件
 * @version V1.0
 */
public class MybatisGeneratorUtil {
	
	private static final String CONFIG_FILE_PATH = "/resources/mybatis/generatorConfig.xml";
	
	public static void main(String[] args) {
		MyBatisGenerator myBatisGenerator = null;
		Configuration config = null;
		boolean overwrite = Boolean.TRUE;
		
		try {
			System.out.println("读取配置文件......");
			System.out.println("根据配置文件开始生成entity、mapper、xml......");
			List<String> warnings = new ArrayList<String>();
			File configFile = new File(MybatisGeneratorUtil.class.getResource(CONFIG_FILE_PATH).getFile());
			ConfigurationParser cp = new ConfigurationParser(warnings);
			config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			
			myBatisGenerator.generate(null);
			System.out.println("执行完毕！请刷新包目录");
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XMLParserException e) {
			e.printStackTrace();
		}catch (InvalidConfigurationException e) {
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
